package com.heima.controller;

import com.heima.pojo.Role;
import com.heima.pojo.User;
import com.heima.pojo.dto.RoleDto;
import com.heima.pojo.dto.UserDto;
import com.heima.pojo.dto.UserPageDto;
import com.heima.pojo.dto.UserSearchDto;
import com.heima.pojo.vo.UserPageVo;
import com.heima.pojo.vo.UserSearchPageVo;
import com.heima.pojo.vo.UserSearchVo;
import com.heima.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/api/user-service/user")
@Api("人员接口")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 人员列表查询
     * @return
     */
    @GetMapping("/search")
    @ApiOperation("人员列表查询")
    public UserPageVo pageQuery(UserPageDto dto) {
        log.info("dto:{}", dto);
        UserPageVo userPageVo = userService.pageQuery(dto);
        return userPageVo;
    }

    /**
     * 新增人员
     */
    @PostMapping
    @ApiOperation("新增人员")
    public Boolean add(@RequestBody @Validated UserDto dto){
        log.info("dto:{}",dto);
        userService.save(dto);
        return true;
    }

    /**
     * 修改人员
     */
    @PutMapping("/{userId}")
    @ApiOperation("修改人员")
    public Boolean update(@PathVariable Integer userId,@RequestBody @Validated UserDto dto){
        log.info("dto:{},id:{}",dto,userId);
        dto.setId(userId.toString());
        userService.save(dto);
        return true;
    }

    /**
     * 删除人员
     */
    @DeleteMapping("/{userId}")
    public Boolean delById(@PathVariable Integer userId){
        log.info("id:{}",userId);
        userService.delById(userId.toString());
        return true;
    }

    /**
     * 获取用户基本信息
     */
    @GetMapping("/{id}")
    public User queryUserInfo(@PathVariable String id){
        log.info("id:{}",id);
        return userService.queryById(id);
    }

    /**
     * 人员工作量列表
     */
    @GetMapping("/searchUserWork")
    public UserSearchPageVo userSearch(UserSearchDto dto){
        log.info("dto:{}",dto);
        return userService.userSearch(dto);
    }

    /**
     * 根据售货机获取运营人员列表
     */
    @GetMapping("/operatorList/{innerCode}")
    public List<UserSearchVo> queryListByInnerCode(@PathVariable String innerCode){
        log.info("innerCode:{}",innerCode);
        return userService.queryListByInnerCode(innerCode);
    }
}
